<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 * 银豆,银豆兑换
 */
 
namespace osc\mobile\controller;
use think\Db;
class Points extends MobileBase
{
	protected function _initialize(){
		parent::_initialize();			
	}
	
	function index(){
		$this->assign('SEO',['title'=>'银豆专区']);		
		$this->assign('top_title','银豆专区');
		return $this->fetch();
	}
	
	public function ajax_goods_list(){

        $page=input('param.page');//页码

        $limit = (6 * $page) . ",6";
		
        $list=Db::name('goods')->where('type',3)->order('goods_id desc')->limit($limit)->select();
		
		if(isset($list)&&is_array($list)){
				foreach ($list as $k => $v) {				
					$list[$k]['image']=resize($v['image'], 250, 250);		
				}
		}
        return  $list;
    }
	//商品详情
	function detail(){

		if(!$list=osc_goods()->get_goods_info((int)input('param.id'))){
			$this->error('商品不存在');
		}
		$rule = Db::name('goods_attribute')
				->where(['goods_id'=>input('param.id')])
				->select();
		osc_goods()->update_goods_viewed((int)input('param.id'));
		$this->assign('SEO',array('title'=>'银豆兑换-'.$list['goods']['name'].'-'.config('SITE_TITLE'),'keywords'=>$list['goods']['name'],'description'=>$list['goods']['name']));
		$this->assign('rule',$rule);
		$this->assign('top_title',$list['goods']['name']);
		$this->assign('goods',$list['goods']);
		$this->assign('image',$list['image']);
		$this->assign('options',$list['options']);

		//数据量大的时候，此处会有性能问题，视情况进行修改
		$this->assign('related_goods',Db::name('goods')->where(array('status'=>1,'type'=>config('common_goods')))->field('goods_id,image,name')->order('rand()')->limit('6')->select());
		if(in_wechat())
			$this->assign('signPackage',wechat()->getJsSign(request()->url(true)));
		$uid=is_login();
		$cart_list=Db::name('cart')->where(array('uid'=>$uid,'type'=>'1'))->select();
		$count = sizeof($cart_list);
		//var_dump($cart_list);
		$this->assign('count',$count);

		return $this->fetch('detail');
	}

	/**
	 * 请求获取规格相对应的价格
	 * @return array
	 */
	function get_attribute_money(){
		$data = $this->request->post();
		$info = Db::name('goods_attribute')
				->where(['attribute_value_id'=>$data['attribute_id'],'goods_id'=>$data['goods_id']])
				->find();
		if($info){
			return ['code'=>0,'success'=>'ok','data'=>$info];

		}
		return array('code'=>-1,'success'=>'请求失败');
	}



	function points_list(){
		$this->assign('user_info',Db::name('member')->where(array('uid'=>member('uid')))->find());	
		$this->assign('list',Db::name('points')->where(array('uid'=>member('uid')))->select());		
		$this->assign('empty',"<span style='margin-left:20px;'>没有数据</span>");
		$this->assign('top_title','我的银豆');
		$this->assign('SEO',['title'=>'我的银豆']);
		return $this->fetch();
	}
	
	function add(){
		
		if(!$uid=is_login()){
			return array('error'=>'请先登录','url'=>url('Login/login'));
		}
		$cart=osc_cart();

		$post=input('post.');
		$param['goods_id']=$post['goods_id'];
		$param['quantity']=1;
		$param['option']=isset($post['option'])?$post['option']:'';
		
		//验证商品数量和必选项
		if($error=$cart->check_quantity($param)){			
			return $error;
		}		

		$cart_data['uid']=$uid;
		$cart_data['goods_id']=(int)$param['goods_id'];
		$cart_data['quantity']=1;		
		$cart_data['attribute_value_id']=$post['attribute_id'];
		$cart_data['type']=config('points_goods');
		$cart_data['goods_option']= isset($post['option'])?json_encode(array_filter($post['option'])):'';
		if($cart_old=Db::name('cart')->where($cart_data)->find()){//已经存在数据
		
			return array('success'=>url('Mobile/Points/checkout',
			array('id'=>hashids()->encode($cart_old['cart_id']))));
			
		}else{
		//插入一条数据			
			$cart_data['create_time']=time();	
					
			if($cart_id=Db::name('cart')->insert($cart_data,false,true)){
				return array('success'=>url('Mobile/Points/checkout',
				array('id'=>hashids()->encode($cart_id))));
				
			}else{
				return array('error'=>'购买失败');
			}
		}
		
	}
	
	function checkout(){
				
		if(!$uid=is_login()){
			return array('error'=>'请先登录');
		}	
		
		$cart=osc_cart();
		
		$id=hashids_decode(input('param.id'));
		$goods=Db::name('cart')->where('cart_id',$id)->find();
//		print_r($goods);exit;
		if(!$id||!$goods){
			$this->error('非法操作');
		}		
						
		$address_id=(int)member('address_id');
		//配送地址		
		$address=Db::name('address')->where('address_id',$address_id)->find();				
		//计算运费
		if(isset($address)){
			$this->assign('address',$address);				
		}
		
		$points_goods=Db::name('goods')->find($goods['goods_id']);

//		print_r($points_goods);exit;
		$goods_option=osc_cart()->get_cart_options($goods['goods_id'],json_decode($goods['goods_option']));

		$this->assign('option',$goods_option);
		$this->assign('cart',$goods);
		$this->assign('goods',$points_goods);		

		$this->assign('SEO',['title'=>'银豆专区-'.config('SITE_TITLE')]);
		$this->assign('top_title','银豆专区');
		return $this->fetch();
	
	}
	
	
	//银豆下单
	function payment(){
		if(request()->isPost()){
		
			$service=osc_service('common','points');
			
			$post=input('post.');
			$param['cart_id']=hashids_decode($post['id']);
			if(!$param['cart_id']){
				$this->error('非法操作');
			}
			//用户名
			$param['username']=member('username');		
			//邮件
			$param['email']=member('email');			
			//用户id
			$param['uid']=member('uid');		
			//收货地址
			$param['shipping_address_id']=$post['address_id'];
			//供货商
			$param['agent_id']=$post['agent_id'];
			//用户剩余银豆
			$param['user_points']=member('points');			
			//送货方式（快递）
			$param['shipping_method']=config('default_transport_id');	
			//支付方式		
			$param['payment_method']='points';
			//重量
			$param['weight']=0;
			//收货城市
			$param['shipping_city_id']=$post['city_id'];	
			//留言
			$param['comment']=$post['comment'];
			//商品类型
			$param['type']=config('points_goods');
			//所选规格
			$param['type']=config('points_goods');
			//数据验证
			$validate=$service->validate_order_data($param);
			if(isset($validate['error'])){
				return $validate;
			}
			//下单
			$service->add_order($param);

			return array('success'=>url("order/silver"));
		}
	}
	
}
